/*

 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2016年10月20日
 * <修改描述:>
 */
package com.tx.core.ddlutil.executor.impl;

import com.tx.core.ddlutil.executor.AbstractTableDDLExecutor;
import com.tx.core.starter.component.DatabaseTypeEnum;

import javax.sql.DataSource;

/**
 * Mysql 表相关 DDL执行器<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2016年10月20日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MysqlTableDDLExecutor extends AbstractTableDDLExecutor {

    //根据表名判断表是否存在.
    /*    private static final String SQL_TABLE_IS_EXISTS = "SELECT COUNT(1) " +
                "FROM INFORMATION_SCHEMA.`TABLES` TAB "
                + "WHERE " +
                "TAB.TABLE_NAME = ? " +
                "AND TAB.TABLE_SCHEMA = ? " +
                "AND TAB.TABLE_TYPE='BASE TABLE'";*/

    //查询table数据中的实际定义
    /*    private static final String SQL_FIND_TABLE_BY_TABLENAME = "SELECT "
                + "TABLE_CATALOG as 'catalog',TABLE_SCHEMA as 'schema',"
                + "TABLE_NAME as 'tableName',TABLE_COMMENT as 'comment' "
                + "FROM INFORMATION_SCHEMA.`TABLES` TAB "
                + "WHERE TAB.TABLE_NAME = ? AND TAB.TABLE_SCHEMA = ? AND TAB.TABLE_TYPE='BASE TABLE'";*/

    //查询出的table定义的RowMapper
    //    private static final RowMapper<DBTableDef> ddlTableRowMapper = new BeanPropertyRowMapper<DBTableDef>(
    //            DBTableDef.class);

    //查询table中column定义
    /*    private static final String SQL_QUERY_COLUMN_BY_TABLENAME = "SELECT "
                + "TCOL.COLUMN_NAME AS 'columnName', "
                + "TCOL.COLUMN_TYPE AS 'columnType', "
                + "TCOL.TABLE_NAME AS 'tableName', "
                + "( CASE WHEN TCOL.COLUMN_KEY = 'PRI' THEN 'Y' ELSE 'N' END ) AS 'primaryKey', "
                + "( CASE WHEN TCOL.IS_NULLABLE = 'NO' THEN 'Y' ELSE 'N' END ) AS 'required', "
                + "( CASE WHEN TCOL.DATA_TYPE = 'int' THEN 'integer' ELSE TCOL.DATA_TYPE END ) AS 'jdbcType', "
                + "( CASE WHEN TCOL.CHARACTER_MAXIMUM_LENGTH IS NULL THEN TCOL.NUMERIC_PRECISION ELSE TCOL
                .CHARACTER_MAXIMUM_LENGTH END ) AS size, "
                + "TCOL.NUMERIC_SCALE AS 'scale', " + "( CASE "
                + "     WHEN TCOL.DATA_TYPE = 'bit' AND TCOL.COLUMN_DEFAULT = 'b''1''' THEN 1 "
                + "     WHEN TCOL.DATA_TYPE = 'bit' AND TCOL.COLUMN_DEFAULT = 'b''0''' THEN 0 "
                + "     WHEN TCOL.DATA_TYPE = 'datetime' AND TCOL.COLUMN_DEFAULT = 'CURRENT_TIMESTAMP' THEN 'now()' "
                + "     WHEN TCOL.DATA_TYPE = 'varchar' THEN CONCAT( '''', TCOL.COLUMN_DEFAULT, '''' ) "
                + "     ELSE TCOL.COLUMN_DEFAULT END " + ") AS 'defaultValue' "
                + "FROM INFORMATION_SCHEMA.`COLUMNS` TCOL "
                + "WHERE TCOL.TABLE_NAME = ? AND TCOL.TABLE_SCHEMA = ?";*/

    //查询出的column定义的RowMapper
    /*    private static final RowMapper<DBColumnDef> ddlColumnRowMapper = new RowMapper<DBColumnDef>() {
            @Override
            public DBColumnDef mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                DBColumnDef col = new DBColumnDef();
                col.setColumnName(rs.getString("columnName"));
                col.setTableName(rs.getString("tableName"));

                col.setPrimaryKey(rs.getBoolean("primaryKey"));
                col.setRequired(rs.getBoolean("required"));
                col.setSize(rs.getInt("size"));
                col.setScale(rs.getInt("scale"));

                String jdbcTypeString = rs.getString("jdbcType");
                JdbcTypeEnum jdbcType = JdbcTypeEnum.forTypeKey(jdbcTypeString);
                AssertUtils
                        .notNull(jdbcType, "jdbcType is null.:{}", jdbcTypeString);
                col.setJdbcType(jdbcType);

                String defaultValue = rs.getString("defaultValue");
                switch (jdbcType) {
                    case DATE:
                        if (StringUtils.equals("CURRENT_TIMESTAMP", defaultValue)) {
                            defaultValue = "now()";
                        }
                        break;
                    default:
                        if (StringUtils.equals("b'1'", defaultValue)) {
                            defaultValue = "1";//"true";
                        } else if (StringUtils.equals("b'0'", defaultValue)) {
                            defaultValue = "0";//"false";
                        } else if (StringUtils.equals("''", defaultValue)) {
                            defaultValue = "";
                        }
                        break;
                }
                col.setDefaultValue(defaultValue);
                return col;
            }
        };*/

    //查询索引SQL
    /*    private static final String SQL_QUERY_INDEX_BY_TABLENAME = " SELECT T.indexName,T.tableName,T.uniqueKey,"
                + "GROUP_CONCAT(T.columnName ORDER BY orderPriority asc) AS columnNames from ( "
                + "SELECT " + "TIDX.INDEX_NAME AS 'indexName', "
                + "TIDX.COLUMN_NAME AS 'columnName', "
                + "TIDX.TABLE_NAME AS 'tableName', "
                + "(CASE WHEN TIDX.NON_UNIQUE = 1 THEN 0 ELSE 1 END ) AS 'uniqueKey', "
                + "TIDX.SEQ_IN_INDEX as 'orderPriority' "
                + "FROM information_schema.`STATISTICS` TIDX "
                + "LEFT JOIN information_schema.`TABLE_CONSTRAINTS` TCONS ON (TIDX.TABLE_SCHEMA = TCONS
                .CONSTRAINT_SCHEMA AND TIDX.TABLE_NAME = TCONS.TABLE_NAME AND TIDX.INDEX_NAME = TCONS.CONSTRAINT_NAME) "
                + "WHERE TIDX.TABLE_NAME = ? AND TIDX.TABLE_SCHEMA = ? "
                + "AND (TCONS.CONSTRAINT_TYPE is null or 'PRIMARY KEY' <> TCONS.CONSTRAINT_TYPE) "
                + "ORDER BY TIDX.INDEX_NAME,TIDX.SEQ_IN_INDEX "
                + ") T GROUP BY indexName,tableName,uniqueKey ";*/

    //DDLindexRowMap
    /*    private static final RowMapper<DBIndexDef> ddlIndexRowMapper = new RowMapper<DBIndexDef>() {
            @Override
            public DBIndexDef mapRow(ResultSet rs, int rowNum) throws SQLException {
                DBIndexDef ddlIndex = new DBIndexDef();
                ddlIndex.setName(rs.getString("indexName"));
                ddlIndex.setTableName(rs.getString("tableName"));
                ddlIndex.setUniqueKey(rs.getBoolean("uniqueKey"));
                ddlIndex.setColumnNames(rs.getString("columnNames"));
                return ddlIndex;
            }
        };*/


    public MysqlTableDDLExecutor(DataSource dataSource) {
        super(dataSource);
    }

    /** <默认构造函数> */
    public MysqlTableDDLExecutor() {
        super();
    }

    /**
     * 从当前数据库中获取当前表定义<br/>
     * <功能详细描述>
     * @param tableName
     * @return [参数说明]
     *
     * @return DDLTable [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    /*@Override
    public DBTableDef findDBTableDetailByTableName(String tableName) {
        AssertUtils.notEmpty(tableName, "tableName is empty.");

        //List<DBTableDef> ddlTableList = this.jdbcTemplate.query(
        //        SQL_FIND_TABLE_BY_TABLENAME,
        //        new Object[] { tableName, this.schema },
        //        ddlTableRowMapper);
        List<DBTableDef> ddlTableList = this.jdbcTemplate.query(
                SQL_FIND_TABLE_BY_TABLENAME,
                ddlTableRowMapper,
                tableName,
                this.schema);

        if (CollectionUtils.isEmpty(ddlTableList)) {
            return null;
        }

        DBTableDef ddlTable = ddlTableList.get(0);
        List<DBColumnDef> columns = queryDBColumnsByTableName(tableName);
        List<DBIndexDef> indexes = queryDBIndexesByTableName(tableName);
        ddlTable.setColumns(columns);
        ddlTable.setIndexes(indexes);

        return ddlTable;
    }*/

    /**
     * 从当前数据库中获取当前表定义<br/>
     * <功能详细描述>
     * @param tableName
     * @return [参数说明]
     *
     * @return DDLTable [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    /*@Override
    public DBTableDef findDBTableByTableName(String tableName) {
        AssertUtils.notEmpty(tableName, "tableName is empty.");

        //List<DBTableDef> ddlTableList = this.jdbcTemplate.query(
        //        SQL_FIND_TABLE_BY_TABLENAME,
        //        new Object[] { tableName, this.schema },
        //        ddlTableRowMapper);
        List<DBTableDef> ddlTableList = this.jdbcTemplate.query(
                SQL_FIND_TABLE_BY_TABLENAME,
                ddlTableRowMapper,
                tableName,
                this.schema);

        if (CollectionUtils.isEmpty(ddlTableList)) {
            return null;
        }
        DBTableDef ddlTable = ddlTableList.get(0);
        return ddlTable;
    }*/

    /**
     * 根据表名查询DDL的字段集合<br/>
     * <功能详细描述>
     * @param tableName
     * @return [参数说明]
     *
     * @return List<DDLColumn> [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    /*@Override
    public List<DBColumnDef> queryDBColumnsByTableName(String tableName) {
        AssertUtils.notEmpty(tableName, "tableName is empty.");

        //List<DBColumnDef> ddlColumnList = this.jdbcTemplate.query(
        //        SQL_QUERY_COLUMN_BY_TABLENAME,
        //        new Object[] { tableName, this.schema },
        //        ddlColumnRowMapper);
        List<DBColumnDef> ddlColumnList = this.jdbcTemplate.query(
                SQL_QUERY_COLUMN_BY_TABLENAME,
                ddlColumnRowMapper,
                tableName,
                this.schema);

        return ddlColumnList;
    }*/

    /**
     * 根据表名查询DDL的索引集合<br/>
     * <功能详细描述>
     *
     * @param tableName
     * @return [参数说明]
     * @return List<DDLIndex> [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    /*@Override
    public List<DBIndexDef> queryDBIndexesByTableName(String tableName) {
        AssertUtils.notEmpty(tableName, "tableName is empty.");

        //List<DBIndexDef> ddlIndexList = this.jdbcTemplate.query(
        //        SQL_QUERY_INDEX_BY_TABLENAME,
        //        new Object[] { tableName, this.schema },
        //        ddlIndexRowMapper);
        List<DBIndexDef> ddlIndexList = this.jdbcTemplate.query(
                SQL_QUERY_INDEX_BY_TABLENAME,
                ddlIndexRowMapper,
                tableName,
                this.schema);
        return ddlIndexList;
    }*/
    @Override
    public DatabaseTypeEnum getDatabaseType() {
        return DatabaseTypeEnum.MySQL;
    }

}
